﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using System.Linq;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Utils.Images;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Core.Config;

namespace Framework.Portal.Layouts.Client.Modules.Menu
{
    public partial class TeenioShow : BaseControl
    {
        public TeenioShow()
        {
        }

        public TeenioShow(JSection section)
        {
            if(!string.IsNullOrEmpty(section.parameter))
            {
                string[] arrParams = section.parameter.Split('&');
                if (arrParams.Length > 0)
                    menuId = Convert.ToInt32(arrParams[0].Split('=')[1]);
                if (arrParams.Length > 1)
                    catId = Convert.ToInt32(arrParams[1].Split('=')[1]);
            }
            MENUNAME = section.title;
        }
        public string MENUNAME = string.Empty;

        protected static int catId = 0;
        protected static int menuId = 0;
        protected static int curMenu = 0;
        protected int articleId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hplViewMore.NavigateUrl = cfg.MenuPath(Configurations.PORTALID, menuId);
                int totalRecord;
                List<TeenioContent> lstContent = ManagerFactory.TeenioContentManager.GetList(catId, 3, 2, 0, 0, 7, out totalRecord);
                if (lstContent != null)
                {
                    if (!string.IsNullOrEmpty(lstContent[0].CONTENT_AVATAR))
                        imgAvatar.ImageUrl = ImagesUtils.GetImage(lstContent[0].CONTENT_AVATAR, 110, 90);
                    lblCreateDate.Text = DateTimeHelper.formatDateTime(lstContent[0].CONTENT_MODIFYDDATE);
                    hplTitle.Text = lstContent[0].CONTENT_HEADLINE;
                    hplTitle.NavigateUrl = cfg.ArticlePath(lstContent[0].CONTENT_PORTALID, lstContent[0].MENUID, lstContent[0].CONTENT_ID);
                    lblDescription.Text = ConvertUtility.ToSubString(lstContent[0].CONTENT_TEASER, 260);
                    hplComment.NavigateUrl = cfg.ArticlePath(lstContent[0].CONTENT_PORTALID, lstContent[0].MENUID, lstContent[0].CONTENT_ID);
                    articleId = lstContent[0].CONTENT_ID;
                    if (lstContent.Count > 1)
                    {
                        rptContent.DataSource = lstContent.Skip(1);
                        rptContent.DataBind();
                    }
                }
                else
                    pnlContent.Visible = false;
            }
        }

        protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (TeenioContent)e.Item.DataItem;
                var imgAvatar = (Image)e.Item.FindControl("imgAvatar");
                var hplTitle = (HyperLink)e.Item.FindControl("hplTitle");
                var lblCreateDate = (Literal)e.Item.FindControl("lblCreateDate");

                if (!string.IsNullOrEmpty(info.CONTENT_AVATAR))
                    imgAvatar.ImageUrl = ImagesUtils.GetImage(info.CONTENT_AVATAR, 80, 65);
                hplTitle.Text = ConvertUtility.ToSubString(info.CONTENT_HEADLINE, 40);
                hplTitle.NavigateUrl = cfg.ArticlePath(info.CONTENT_PORTALID, info.MENUID, info.CONTENT_ID);
                lblCreateDate.Text = DateTimeHelper.formatDateTime(info.CONTENT_MODIFYDDATE);
            }
        }
    }
}